<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Model\ChargeDetail;
use App\Model\ChargeOption;
use App\Model\Device;
use App\Model\Distributors;
use App\Model\Order;
use App\Model\Station;
use App\Model\User;
use App\Services\FileService;
use Carbon\Carbon;
use Hyperf\HttpMessage\Upload\UploadedFile;
use Hyperf\Redis\Redis;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RedisException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Util\Common;
use App\Controller\AbstractController;
use PhpOffice\PhpSpreadsheet\IOFactory;

class StaticsController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Device $device;


    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**获取后台背景图
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getBackImage():ResponseInterface
    {
        $data['url'] = $this->redis->get('back_admin_img');
        return $this->success($data);
    }

    /**卡片统计
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function scrollNum():ResponseInterface{
        $data['members'] = User::count();
        $data['devices'] =Device::count();
        $data['orders'] = Order::where('status',Order::STATUS_END)
            ->count();
        $allKey = 'ALL_ORDER_PROFIT';
        if($this->redis->exists($allKey)){
            $data['profit'] = $this->redis->get($allKey);
            $data['profit'] = number_format((float)$data['profit'],2);
        }else{
            $this->redis->set($allKey,'0.15');
        }
        return $this->success($data);
    }

    /**获取chartData
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function chartData():ResponseInterface{
        $now = new \DateTime();
        $startOfDay = clone $now;
        $startOfDay->setTime(0, 0, 0);
        $now->setTime(23, 59, 59);
        $i = 0;
        $data['expectedData'] =[];
        while ($startOfDay <= $now) {
            if ($i > 24) {
                break;
            }
            $start= $startOfDay->format('Y-m-d H:i:s');
            // 移动到下一个小时
            $end = $startOfDay->modify('+1 hour')->format('Y-m-d H:i:s');
            $powerfee = Order::where('stop_at','>=',$start)
                ->where('stop_at','<=',$end)
                ->sum('fee');
            $returnfee = Order::where('stop_at','>=',$start)
                ->where('stop_at','<=',$end)
                ->sum('return_fee');
            $insideFee = number_format($powerfee-$returnfee,2);
            $data['expectedData'][] = $insideFee;
//            $data['now_total'] += $tempArr['power'];
            $i++;
        }
        return $this->success($data);
    }

    /**获取pieChart
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function pieData():ResponseInterface{
        $data['pieChartData'] =[];
        $array= [1,2];
        foreach($array as $k => $v){
            if($v == 1){
                $data['pieChart'][] ='微信用户';
                $data['pieChartData'][$k]['name'] = '微信用户';
            }else{
                $data['pieChart'][] ='支付宝用户';
                $data['pieChartData'][$k]['name'] = '支付宝用户';
            }
            $data['pieChartData'][$k]['value']  = User::where('login_type',$v)
                ->count();
        }
        return $this->success($data);
    }

    /**获取lineChart
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getMonth():ResponseInterface{
            $data['line'] = [];
            $data['data'] =[];
            for($i=1;$i<=12;$i++){
                if($i<10){
                    $start= date("Y-0{$i}-01 00:00:00");
                    $end= date("Y-0".($i+1)."-01 00:00:00");
                }else{
                    $start= date("Y-{$i}-01 00:00:00");
                    $end= date("Y-".($i+1)."-01 00:00:00");
                }
                array_push($data['line'],"{$i}月");
                 $fee= Order::where('stop_at','>=',$start)
                    ->where('stop_at','<=',$end)
                    ->sum('fee');
                 $refund = Order::where('stop_at','>=',$start)
                     ->where('stop_at','<=',$end)
                     ->where('is_return_fee',1)
                     ->sum('return_fee');
                $data['data'][]  = number_format($fee-$refund,2);
            }
         return $this->success($data);
    }
}
